define(function (){
	
	return{
			//找出是属于哪个子网络拓扑
			findNetMap:function(appIps,netObjs){
				var _netObjs  = new Array();
				for(var i=0;i<netObjs.length;i++)
				{
					var netRoot = netObjs[i].root;
					for(var j=0;j<appIps.length;j++)
					{
						var fl = false;
						for(var l=0;l<netRoot.nodes.node.length;l++)
						{
							if(netRoot.nodes.node[l].asset.ip == appIps[j]){
								!netObjs[i]._id && (netObjs[i]._id=i);
								this.putInArryByRoot(_netObjs,netObjs[i]);
								fl = true;
								break;
							}
						}
						if(fl)
							break;
					}
				}
				console.log(_netObjs);
				return _netObjs;
			},
			putInArryByRoot:function(array,Obj){
				var i=0;
				for(;i<array.length;i++){
					if(array[i].root._x == Obj.root._x && array[i].root._y == Obj.root._y){
						return;
					}
				}
				array[i] = Obj;
			},
			putInArry:function(array,Obj){
				var i=0;
				for(;i<array.length;i++){
					if(array[i] == Obj){
						return;
					}
				}
				array[i] = Obj;
			},
			putInArry2:function(array,Obj){
				var i=0;
				for(;i<array.length;i++){
					if(array[i].asset.ip == Obj.asset.ip){
						return false;
					}
				}
				array[i] = Obj;
				return true;
			},
			putInArry3:function(array,Obj){
				var i=0;
				for(;i<array.length;i++){
					if(array[i]._id == Obj._id){
						return;
					}
				}
				array[i] = Obj;
			},
			//删除非包含关系的所有节点
			findNetFromMaster:function(netObjs,appIps,masterObj)
			{
				if(		appIps == "" || appIps ==undefined || appIps == null || 
						netObjs == "" || netObjs ==undefined || netObjs == null || 
						masterObj == "" || masterObj ==undefined || masterObj == null )
					return null;
				
				var tempNetRoot = {};
				tempNetRoot.nodes = {};
				tempNetRoot.nodes.node = new Array();
				var tempLines = {};
				tempLines.lines = {};
				tempLines.lines.line = new Array();
				//只生成上层拓扑图
				//先归并节点
				for(var i=0;i<netObjs.length;i++)
				{
					var netRoot = netObjs[i].root;
					var tempList = new Array();
					for(var j=0;j<appIps.length;j++)
					{
						for(var k=0;k<netRoot.nodes.node.length;k++)
						{
							if(netRoot.nodes.node[k].asset.ip == appIps[j])
							{
								this.putInArry2(tempList,netRoot.nodes.node[k]);
							}
						}
					}
					for(var j=0;j<netRoot.nodes.node.length;j++)
					{
						for(var k=0;k<masterObj.root.nodes.node.length;k++)
						{
							//找到了
							if(netRoot.nodes.node[j].asset.ip == masterObj.root.nodes.node[k].asset.ip)
							{
								//将其直接下属又未包含在总拓扑图内 的节点加上
								var lines = netRoot.lines.line;
								for(var m=0;m<lines.length;m++)
								{
									var ids = lines[m]._id.split("::");
									var endId = null;
									var style = 1;
									var fid = masterObj.root.nodes.node[k]._id;
									if(ids[0] == netRoot.nodes.node[j]._id)
									{
										endId = ids[1];
									}
									else if(ids[1] == netRoot.nodes.node[j]._id)
									{
										endId = ids[0];
										style = 2;
									}
									for(var n=0;n<tempList.length;n++)
									{
										//重新计算偏移量
										var tx = parseInt(netRoot.nodes.node[j]._x) - parseInt(tempList[n]._x);
										var ty = parseInt(netRoot.nodes.node[j]._y) - parseInt(tempList[n]._y);
										if(tempList[n]._id == endId)
										{
											var adId = "";//k+""+m+""+n;
											tempList[n]._id = tempList[n]._id+adId;
											if(style == 1)
											{
												lines[m]._id = fid+"::"+tempList[n]._id+"::"+ids[2];
												lines[m]._startId = fid;
												lines[m]._endId = tempList[n]._id;
												lines[m].networkLink.startId = fid;
												lines[m].networkLink.endId = tempList[n]._id;
											}
											else{
												lines[m]._id = tempList[n]._id+"::"+fid+"::"+ids[2];
												lines[m]._startId = tempList[n]._id;
												lines[m]._endId = fid;
												lines[m].networkLink.startId = tempList[n]._id;
												lines[m].networkLink.endId = fid;
											}
												
											tempList[n]._x = parseInt(masterObj.root.nodes.node[k]._x)+tx;
											tempList[n]._y = parseInt(masterObj.root.nodes.node[k]._y)+ty;
											flag = this.putInArry2(masterObj.root.nodes.node,tempList[n]);
											if(flag)
												masterObj.root.lines.line[masterObj.root.lines.line.length] = lines[m];
										}
									}
								}
							}
						}
					}
				}
				//删除不相干的节点
				for(var i=0;i<masterObj.root.nodes.node.length;i++)
				{
					for(var j=0;j<netObjs.length;j++)
					{
						var nodes = netObjs[j].root.nodes.node;
						for(var k=0;k<nodes.length;k++)
						{
							if(masterObj.root.nodes.node[i].asset.ip == nodes[k].asset.ip)
							{
								this.putInArry2(tempNetRoot.nodes.node,masterObj.root.nodes.node[i]);
							}
						}
					}
					
				}
				//获取连线
				var lines = masterObj.root.lines.line;
				for(var i=0;i<tempNetRoot.nodes.node.length;i++)
				{
					for(var j=0;j<lines.length;j++)
					{
						var ids = lines[j]._id.split("::");
						var endId = null;
						if(ids[0] == tempNetRoot.nodes.node[i]._id)
						{
							endId = ids[1];
							
						}
						else if(ids[1] == tempNetRoot.nodes.node[i]._id)
						{
							endId = ids[0];
							
						}
						if(endId!=null)
						{
							var f = false;
							for(var k=0;k<tempNetRoot.nodes.node.length;k++)
							{
								if(tempNetRoot.nodes.node[k]._id == endId){
									f = true;
									break;
								}
							}
							//如果没有该节点
							if(f == false)
							{
								
								for(var k=0;k<masterObj.root.nodes.node.length;k++)
								{
									if(masterObj.root.nodes.node[k]._id == endId)
									{
										console.log("line id is " +lines[j]._id);
										this.putInArry2(tempNetRoot.nodes.node,masterObj.root.nodes.node[i]);
										break;
									}
								}
							}
							else
								this.putInArry3(tempLines.lines.line, lines[j]);
						}
						
					}
				}
				console.log("返回数据");
				return [tempNetRoot,tempLines];
			},
			//检查两点之间的连通性
			isLinked:function(masterObj,startIp,endIp){
				
			},
	}
})